<template>
  <div>
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" class="demo-ruleForm" label-width="100px" style="width: 500px">

      <el-form-item label="用户id" prop="id">
        <el-input v-model="ruleForm.id" readonly></el-input>
      </el-form-item>

      <el-form-item label="真实姓名" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>

      <el-form-item label="账号" prop="account">
        <el-input v-model="ruleForm.account"></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password"></el-input>
      </el-form-item>

      <el-form-item label="联系方式" prop="connection">
        <el-input v-model="ruleForm.connection"></el-input>
      </el-form-item>

      <el-form-item label="用户类型" prop="typeid">
        <el-input v-if="ruleForm.typeid==1" readonly value="云工厂管理员"></el-input>
        <el-input v-if="ruleForm.typeid==2" readonly value="经销商"></el-input>
        <el-input v-if="ruleForm.typeid==3" readonly value="系统管理员"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">确认修改</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      ruleForm: {
        id:null,
        name: null,
        account: null,
        password: null,
        typeid:null,
        connection:null
      },
      rules: {
        name: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        account: [
          { required: true, message: '账号不能为空', trigger: 'change' }
        ],
        password:[
          { required: true, message: '密码不能为空', trigger: 'change' },
          { min: 5, max: 20, message: '长度在 5 到 20 个字符', trigger: 'blur' }
        ],
        connection:[
          { required: true, message: '联系方式不能为空', trigger: 'change' }
        ]
      },
    };
  },created() {
    axios.get("http://localhost:8081/user/findById/"+this.$route.query.id).then((response)=>{
      this.ruleForm = response.data
    })
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.post("http://localhost:8081/user/update",this.ruleForm).then((response)=>{
            if(response.data == "success"){
              this.success();
              this.$router.push("/userList");
            }else{
              this.failed();
            }
          })
        } else {
          this.failed();
          return false;
        }
      });
    },
    resetForm(formName) {
      axios.get("http://localhost:8081/user/findById/"+this.$route.query.id).then((response)=>{
        this.ruleForm = response.data
      })
    },
    success() {
      this.$message({
        message: '修改成功!',
        type: 'success'
      });
    },
    failed() {
      this.$message.error('修改失败，请检查内容是否正确或稍后再试!');
    }
  }
}
</script>